package com.hollysys.hollimsa.common.tools;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;

/**
 * @author wangyuxi
 * @date 2020/11/12
 **/
public final class ResponseUtil {

    private ResponseUtil(){
        //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
        //see 《Effective Java》 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }


    public static void writeJson(HttpServletResponse response,String json){

        response.setContentType("application/json; charset=utf-8");

        response.setCharacterEncoding(StandardCharsets.UTF_8.name());

        try(PrintWriter printWriter = response.getWriter()){
            printWriter.print(json);
            printWriter.flush();
            //http://www.iteye.com/problems/56543
            //你是用了tomcat,jetty这样的容器,就不需要 printWriter.close()
        }catch (IOException e){
            throw new UncheckedIOException(e);
        }
    }




}
